//
//  WebViewViewController.swift
//  DemoUI
//  网页视图应用
//  Created by 白瑾浩 on 2022/1/8.
//

import UIKit
import JavaScriptCore

class WebViewViewController: UIViewController, UIWebViewDelegate {
    var webView:UIWebView?
    var buttonGoBack:UIButton?, buttonForward:UIButton?
    var jsContext:JSContext?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        //WKWebView
        
        //普通使用 直接简单装载视图
        //        let webView = UIWebView(frame: self.view.bounds) //bounds屏幕大小  父类边界大小
        //
        //        let url = URL(string: "https://www.baidu.com")
        //        //创建请求
        //        let request = URLRequest(url: url!)
        //        webView.loadRequest( request )
        //        self.view.addSubview( webView )
        //
        //
        //复杂使用 有上一页下一页监听
        
        webView = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height-30)) //bounds屏幕大小  父类边界大小
        
        let url = URL(string: "https://www.baidu.com")
        //创建请求
        let request = URLRequest(url: url!)
        webView?.loadRequest( request )
        webView?.delegate = self
    
        self.view.addSubview( webView! )
        
    

//        let settingsBundlePath = Bundle.main.path(forResource:"Settings", ofType:"bundle")
//
//        let path = Bundle.init(path: settingsBundlePath! )
//        //print( "path\(path)" )
//        let path2 = path?.path(forResource:"index", ofType:"html", inDirectory:"") //目录
//
//        let html = try? String(contentsOfFile: path2!, encoding: String.Encoding.utf8)
//
//        webView?.loadHTMLString(html!, baseURL: nil)
        
        let toolView = UIView(frame: CGRect(x: 0, y: self.view.frame.height-30, width: self.view.frame.width, height: 30))
        toolView.backgroundColor = UIColor.purple
        self.view.addSubview( toolView )
        
        
        //添加2个按钮
        
        //后退
        buttonGoBack = UIButton(frame: CGRect(x: 30, y: 0, width: 70, height: 30))
        buttonGoBack?.setTitle("后退", for: UIControl.State.normal)
        buttonGoBack?.setTitleColor(UIColor.lightGray, for: UIControl.State.disabled)
        buttonGoBack?.addTarget(self, action: #selector(goback), for: UIControl.Event.touchUpInside)
        
        //前进
        buttonForward = UIButton(frame: CGRect(x: self.view.frame.width-100, y: 0, width: 70, height: 30))
        buttonForward?.setTitle("前进", for: UIControl.State.normal)
        buttonForward?.setTitleColor(UIColor.lightGray, for: UIControl.State.disabled)
        buttonForward?.addTarget(self, action: #selector(forward), for: UIControl.Event.touchUpInside)
        
        toolView.addSubview( buttonGoBack! )
        toolView.addSubview( buttonForward! )
    }
    //后退
    @objc func goback(){
        webView!.goBack()
    }
    //向前
    @objc func forward(){
        webView!.goForward()
    }

        
    func webViewDidStartLoad(_ webView: UIWebView) {
        
    }
    func webViewDidFinishLoad(_ webView: UIWebView) {
        //根据是否有前一页 而来判断按钮是否可点击
        buttonForward?.isEnabled = webView.canGoForward
        //根据是否有后一页 而来判断按钮是否可点击
        buttonGoBack?.isEnabled = webView.canGoBack
        
        
        //调用html中js 方法
        webView.stringByEvaluatingJavaScript(from: "test()")
        
        
        self.jsContext = self.webView!.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as? JSContext
        let model = VideoJsModel()
        model.jsContext = self.jsContext
        
        self.jsContext?.setObject(model, forKeyedSubscript: "Test"  as NSCopying & NSObjectProtocol)
        
        
        
        let settingsBundlePath = Bundle.main.path(forResource:"Settings", ofType:"bundle")
       
        let path = Bundle.init(path: settingsBundlePath! )
        //print( "path\(path)" )
        let path2 = path?.path(forResource:"index", ofType:"html", inDirectory:"") //目录
        
        let html = try? String(contentsOfFile: path2!, encoding: String.Encoding.utf8)
        
        //执行注册脚本
        self.jsContext?.evaluateScript( html )

        self.jsContext?.exceptionHandler = {

            (context, exception) in

            print("exception 错误@", exception ?? "")

        }
        
        
    }
    
    
}


// 这里必须使用@objc，因为JavaScriptCore库是ObjectiveC版本的。如果不加@objc，则调用无效果
@objc protocol VideoJsDelegate:JSExport {
   

    func callShare()//分享
    func callPhone(_ phone:String)//拨打电话
    func getAddress()
}


@objc class VideoJsModel: NSObject, VideoJsDelegate {
    func getAddress() {
        print( "获取地址了" )
    }


    func callPhone(_ phone: String) {
        print("拨打电话.....")
    }


    func callShare() {
        print( "分享了" )
        
    }

    var jsContext:JSContext!
    
}


